package com.losdun.product.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.losdun.common.utils.sign.Md5Utils;
import com.losdun.product.domain.ProductSku;
import com.losdun.product.domain.vo.SkuVo;
import com.losdun.product.domain.vo.TreeSelectVo;
import com.losdun.product.mapper.ProductSkuMapper;
import com.losdun.product.service.ProductSkuService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sword.ye
 * @date 2025/3/9
 **/
@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements ProductSkuService {

    @Override
    public boolean batchInsert(Long id, List<SkuVo> skus) {
        if(CollectionUtils.isNotEmpty(skus) && id!=null){
            List<ProductSku> productSkus = skus.stream().map(t -> {
                ProductSku sku = new ProductSku();
                sku.setProductId(id);
                sku.setName(t.getName());
                sku.setPrice(t.getPrice());
                sku.setUnit(t.getUnit());
                sku.setSku(t.getSku());
                sku.setImageUrl(t.getImageUrl());
                sku.setAttrJson(JSON.toJSONString(t.getAttributes()));
                sku.setAttrSignature(Md5Utils.hash(t.getSku()));
                return sku;
            }).collect(Collectors.toList());
            return this.saveBatch(productSkus);
        }
        return false;
    }

    @Override
    public void batchUpdate(Long id, List<SkuVo> skus) {
        if(CollectionUtils.isNotEmpty(skus) && id!=null){
            removeByProductId(id);
            this.batchInsert(id, skus);
        }
    }

    @Override
    public List<SkuVo> querySkusVoByProductId(Long productId) {
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSku::getProductId, productId);
        List<ProductSku> productSkuList = this.list(queryWrapper);

        return productSkuList.stream().map(t -> {
            SkuVo skuVo = new SkuVo();
            skuVo.setAttributes(JSON.parseArray(t.getAttrJson(), TreeSelectVo.class));
            skuVo.setPrice(t.getPrice());
            skuVo.setImageUrl(t.getImageUrl());
            skuVo.setUnit(t.getUnit());
            skuVo.setName(t.getName());
            skuVo.setSku(t.getSku());
            return skuVo;
        }).collect(Collectors.toList());
    }

    public void removeByProductId(Long productId) {
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSku::getProductId, productId);
        this.remove(queryWrapper);
    }

    @Override
    public BigDecimal queryMinPrice(List<Long> productIds) {
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductSku::getProductId, productIds)
                .orderByAsc(ProductSku::getPrice).last("limit 1");
        ProductSku minSku = baseMapper.selectOne(queryWrapper);
        return minSku!=null?minSku.getPrice():BigDecimal.ZERO;
    }
}
